# -*- coding: utf-8 -*-

import cgi
import sys, os
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import facebook
from facebook import FacebookError
from google.appengine.ext import db

from bid_funcs  import Utility, FBjqRY,CSS
from bid_db import FbUserProfile, IM , Email , Address , OtherNote , Product ,Price,Trade,FinalPrice,Category,Image

report='test'
FACEBOOK_API_KEY ='fbd2e5652ed8a4eac66ff0ac2d2beb72'
FACEBOOK_SECRET_KEY ='c0857a01dbb16812cfab5b63dcd5e9ea'
_DEBUG = True
g_args={
    'address':'118.169.28.11',
    'DoEditProfile=1':0
        }

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('yes')
    def post(self):
        fb = facebook.Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY)
        if fb.check_session(self.request) and fb.added:
            pass
        else:
            url = fb.get_add_url()
            self.response.out.write('<script language="javascript">top.location.href="' + url + '"</script>')
            return
        g_args['report']=report
        g_args['uid']=fb.uid
        g_args['DoEditProfile']=1
        self.response.out.write(template.render('bid_index.html',{'args':g_args}))

class Sell(webapp.RequestHandler):
    #def get(self):
    def post(self):
        fb = facebook.Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY)
        if fb.check_session(self.request) and fb.added:
            pass
        else:
            url = fb.get_add_url()
            self.response.out.write('<script language="javascript">top.location.href="' + url + '"</script>')
            return
        g_args['DoEditProfile']=1
        #self.response.out.write('work')
        
        self.response.out.write(template.render('sell.html',{'args':g_args}))

class EditProfile(webapp.RequestHandler):
    #def get(self):
    def post(self):
        fb = facebook.Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY)
        if fb.check_session(self.request) and fb.added:
            pass
        else:
            url = fb.get_add_url()
            self.response.out.write('<script language="javascript">top.location.href="' + url + '"</script>')
            return
        g_args['reliable']=80
        g_args['bonusPoints']=100
        self.response.out.write(template.render('editProfile.html',{'args':g_args}))

class SaveProfile(webapp.RequestHandler):
    #def get(self):
    def post(self):
        fb = facebook.Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY)
        if fb.check_session(self.request) and fb.added:
            pass
        else:
            #url = fb.get_add_url()
            #self.response.out.write('<script language="javascript">top.location.href="' + url + '"</script>')
            self.response.out.write("error")
            return
        #self.response.out.write(template.render('editProfile.html',{'args':g_args}))
        self.response.out.write("ok")

        

class Console(webapp.RequestHandler):
    def get(self):
        self.response.out.write('work')
    def post(self):
        fb = facebook.Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY)
        if fb.check_session(self.request) and fb.added:
            pass
        else:
            url = fb.get_add_url()
            self.response.out.write('<script language="javascript">top.location.href="' + url + '"</script>')
            return
        DoEditProfile=1
        self.response.out.write(template.render('console.html',{'DoEditProfile':DoEditProfile}))

def main():
    application = webapp.WSGIApplication([
        ('/',MainPage),
        ('/Utility.js', Utility),
        ('/FBjqRY.min.js', FBjqRY),
        ('/Store.app',Sell),#users, products belong to store, but in the begging ,could be the same
        ('/UserProfile.app',Sell),
        ('/Products.app',Sell),
        ('/Sell.app',Sell),
        ('/Buy.app',Sell),
        ('/Console.app',Console),
        ('/EditProfile.app',EditProfile),
        ('/SaveProfile.app',SaveProfile)
        ],debug=_DEBUG)
    wsgiref.handlers.CGIHandler().run(application)
    
if __name__ == "__main__":
    main()      
